package org.nowcoder.huawei.c200;

import java.util.Arrays;

/**
 * 43:爱吃蟠桃的孙悟空/猴子吃桃
 */
public class HC043 {
    public static void main(String[] args) {
        new HC043().minEatingSpeed(new int[]{312884470}, 968709470);
    }

    public int minEatingSpeed(int[] piles, int h) {
        if (h == piles.length) {
            return Arrays.stream(piles).max().getAsInt();
        }

        int left = 1, right = 1000000000;
        while (left < right) {
            int mid = left + (right - left) / 2;
            int time = 0;
            for (int i = 0; i < piles.length; i++) {
                if (mid >= piles[i]) {
                    time++;
                } else {
                    int temp = piles[i] % mid;
                    time += temp == 0 ? piles[i] / mid : (piles[i] / mid + 1);
                }
            }
            if (time <= h) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }
}
